package flinty.autocomplete.combobox.test;

import java.awt.EventQueue;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.ListCellRenderer;

import flinty.autocomplete.combobox.AutoCompleteComboBoxDecorator;
import flinty.autocomplete.combobox.DataProvider;
import flinty.autocomplete.combobox.ItemWrapper;
import flinty.autocomplete.combobox.StringConverter;
import flinty.autocomplete.combobox.ShortenedListComboBoxModel;
import flinty.autocomplete.combobox.ValueHolder;
import flinty.autocomplete.combobox.AutoCompleteComboBoxDecorator.RendererFactory;
import flinty.autocomplete.combobox.ShortenedListComboBoxModel.SelectObjectCallback;

public class ShortenedModelApplet extends BaseTestApplet {

	private static final long serialVersionUID = -656727212446554013L;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				ShortenedModelApplet frame = new ShortenedModelApplet();
				frame.setVisible(true);
			}
		});
	}

	@Override
	protected void decorateComboBox(JComboBox combo,
			final LinkedList<Integer> lst) {
		AutoCompleteComboBoxDecorator.decorate(combo,
				new ShortenedListComboBoxModel<Integer>(
						new DataProvider<Integer>() {

							@Override
							public List<Integer> getData(String pattern) {
								LinkedList<Integer> result = new LinkedList<Integer>();
								boolean empty = false;
								if (pattern == null
										|| pattern.trim().length() == 0) {
									empty = true;
									result.add(null);
								}
								for (Integer item : lst) {
									if (empty
											|| item.toString()
													.contains(pattern)) {
										result.add(item);
									}
								}
								return result;
							}
						}, new StringConverter<Integer>() {

							@Override
							public String render(Integer object) {
								return object != null ? object.toString()
										: null;
							}
						}, Arrays.asList(null, Integer.valueOf(100), Integer
								.valueOf(101), Integer.valueOf(102)),
						new SelectObjectCallback<Integer>() {

							@Override
							public void selectObject(
									ValueHolder<Boolean> result,
									ValueHolder<Integer> item) {
								try {
									String input = JOptionPane
											.showInputDialog("Enter number");
									if (input == null
											|| input.trim().length() == 0) {
										result.setValue(Boolean.FALSE);
										return;
									}
									Integer number = Integer.valueOf(input);
									if (!lst.contains(number)) {
										JOptionPane.showMessageDialog(null,
												"error: number is out of range, enter for example "
														+ lst.get(0));
										item.setValue(number);
										result.setValue(Boolean.FALSE);
										return;
									}
									item.setValue(number);
									result.setValue(Boolean.TRUE);
								} catch (Exception e) {
									JOptionPane.showMessageDialog(null,
											"error: " + e.getMessage());
									result.setValue(Boolean.FALSE);
								}
							}
						}, "more items"), new RendererFactory() {

					@Override
					public ListCellRenderer createRenderer(
							ListCellRenderer originalRenderer) {
						return originalRenderer;
					}
				}, new ItemWrapper<Integer>(Integer.valueOf(543221), Integer
						.toString(543221)));

	}

	@Override
	public String getAppletInfo() {
		return "shortened list autocomplete demo";
	}
}
